package com.agricultural.iframe;

import com.agricultural.dbchange.ProductDB;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.sql.ResultSet;
import java.util.Locale;

public class PurchaseList {
    private JPanel main;
    private JPanel body;
    private JLabel title;
    private JScrollPane tableBody;
    private JTable tableList;
    private final int userId = Login.user.getId();

    public PurchaseList() {
        initializeComponents();
        initializeTableList();
    }

    private void initializeTableList() {
        // 初始化表格组件
        DefaultTableModel model = new DefaultTableModel();
        // 设置表头
        model.setColumnIdentifiers(new Object[]{"ID", "购买时间", "产品名称", "价格"});
        // 获取产品数据
        try (ResultSet resultSet = ProductDB.getPurchaseListByUserId(userId)) {
            while (resultSet.next()) {
                Object[] rowData = new Object[4];
                rowData[0] = resultSet.getInt(1);
                rowData[1] = resultSet.getString(2);
                rowData[2] = resultSet.getString(3);
                rowData[3] = resultSet.getDouble(4);
                model.addRow(rowData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 添加表格列头
        tableList.setModel(model); // 设置表格的数据模型
        tableList.getColumnModel().getColumn(0).setPreferredWidth(10);
        tableList.getColumnModel().getColumn(2).setPreferredWidth(10);
        tableList.getColumnModel().getColumn(3).setPreferredWidth(10);
    }

    private void initializeComponents() {
        JFrame frame = new JFrame("PurchaseList");
        frame.setSize(600, 500);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setContentPane(main);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        main = new JPanel();
        main.setLayout(new GridLayoutManager(1, 1, new Insets(20, 20, 20, 20), -1, -1));
        body = new JPanel();
        body.setLayout(new FormLayout("fill:d:grow", "center:d:noGrow,top:4dlu:noGrow,center:d:grow"));
        main.add(body, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        title = new JLabel();
        Font titleFont = this.$$$getFont$$$(null, -1, 18, title.getFont());
        if (titleFont != null) title.setFont(titleFont);
        title.setText("我的已购");
        CellConstraints cc = new CellConstraints();
        body.add(title, cc.xy(1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));
        tableBody = new JScrollPane();
        body.add(tableBody, cc.xy(1, 3, CellConstraints.FILL, CellConstraints.FILL));
        tableList = new JTable();
        tableList.setDropMode(DropMode.USE_SELECTION);
        tableList.setEnabled(true);
        Font tableListFont = this.$$$getFont$$$(null, -1, -1, tableList.getFont());
        if (tableListFont != null) tableList.setFont(tableListFont);
        tableList.setShowVerticalLines(true);
        tableBody.setViewportView(tableList);
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
        boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
        Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
        return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return main;
    }

}
